package studentManage.model;

import java.io.Serializable;
import java.util.UUID;

import studentManage.util.MajorUtil;
import studentManage.util.StudentUtil;

public class MajorModel implements Serializable {

	/**
	 * 序列化
	 */
	private static final long serialVersionUID = 1L;

	private Integer id;// id
	private String majCode;// 专业代号
	private String name;// 专业名称
	private String stuId;// 存放一个专业拥有的学生id

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		if (seach(id)) {// true不重复
			this.id = id;
			return;
		}
		System.out.println("id重复，已为您自动生成一个id！");
		this.id = (int) (Math.random() * 2000000000 + 0);
		System.out.println(this.id);
	}

	public String getMajCode() {
		return majCode;
	}

	public void setMajCode(String majCode) {
		if (seach(majCode)) {
			this.majCode = majCode;
			return;
		}
		System.out.println("专业代号重复，已为您自动生成一个代号！");
		this.majCode = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10);
		System.out.println(this.majCode);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String[] getStuId() {
		if (stuId.equals("")) {
			return null;
		}
		StudentUtil studentUtil = new StudentUtil();
		String[] stu = stuId.split(" ");
		for (int i = 0; i < stu.length; i++) {
			stu[i] = studentUtil.selectById(Integer.parseInt(stu[i])).getName();
		}
		return stu;
	}
	
	public void setStuId(String stuId) {
		if (this.stuId == null) {
			this.stuId = stuId;
			return;
		}
		this.stuId = this.stuId + " " + stuId;
	}


	@Override
	public String toString() {
		return "MajorModel [id=" + id + ", majCode=" + majCode + ", name=" + name + ", stuId=" + stuId + "]";
	}

	private boolean seach(Integer id, String majCode) {
		if (id != null) {
			for (StudentModel stu : StudentUtil.students) {
				if (stu.getId() == id)
					return false;
			}
		}
		if (majCode != null) {
			for (MajorModel maj : MajorUtil.majors) {
				if (maj.getMajCode().equals(majCode)) {
					return false;
				}
			}
		}
		return true;
	}

	private boolean seach(Integer id) {
		return seach(id, null);
	}

	private boolean seach(String majCode) {
		return seach(null, majCode);
	}

}
